#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 2e6+10;

pii A[maxn];
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int N, max;cin >> N >> max;
    int num = 0;
    for (int day = 0; day < N; ++day) {
        int n; cin >> n;
        for (int j = 0; j < n; ++j) {
            int a; cin >>a;
            A[num++] = {a, day};
        }
    }
    sort(A, A+num);
    int i = 0, j = 0, ans = maxn;
    unordered_set<int> day;
    while(true)
    {
        while(j<num && day.size()<max)
            day.insert(A[j++].second);
        if(day.size()<max)	break;
        ans=min(ans,A[j-1].first - A[i].first);
        day.erase(A[i++].second);
    }
    cout << ans;
    return 0;
}